package com.example.ycymyapplication;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.provider.Settings;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

public class DengLuActivity extends AppCompatActivity {
    SharedPreferences mySetting,userSetting;
    SharedPreferences.Editor editor;
    EditText et_user,et_pwd;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_deng_lu);
        et_user=findViewById(R.id.et_user);
        et_pwd=findViewById(R.id.et_pwd);
    }

    public void reset(View view) {
        et_user.setText("");
        et_pwd.setText("");
    }
    public void login(View view){
        String user=et_user.getText().toString();
        String pwd=et_pwd.getText().toString();
        if (user==null||user==""){
            Toast.makeText(this, "用户名不能为空", Toast.LENGTH_SHORT).show();
            return;
        }
        if (pwd==null||pwd==""){
            Toast.makeText(this, "密码不能为空", Toast.LENGTH_SHORT).show();
            return;
        }
        userSetting =getSharedPreferences("user",MODE_PRIVATE);
        String userId=userSetting.getString("id","");
        String userPwd=userSetting.getString("pwd","");

        System.out.println("user:"+user+"   pwd:"+pwd+"\n"+"   id:"+userId+"   pwd:"+userPwd);
        if (user.equals(userId)  &&  pwd.equals(userPwd)){
            mySetting =getSharedPreferences("my",MODE_PRIVATE);
            editor = mySetting.edit();
            editor.putString("id",user);
            setResult(1002,new Intent().putExtra("id",user));
            finish();

        }else{
            Toast.makeText(this, "用户名或密码不正确", Toast.LENGTH_SHORT).show();
        }

    }


    public void register(View view){
        startActivityForResult(new Intent(this,ZhuCeActivity.class),1003);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode==1003&&resultCode==1004) {
            if (data != null) {
                String user=data.getStringExtra("id");
                String pwd=data.getStringExtra("pwd");

                et_user.setText(user);
                et_pwd.setText(pwd);
            }
        }
    }
}

